package com.voxel.sense.infomsg.module.task.execution.report.list;


import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.example.com.common.app.BViewActivity;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.view.EmptyView;
import com.example.com.common.view.dialog.DialogCallBack;
import com.example.com.common.view.dialog.DialogImpl;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.interfaces.listener.AddItemPortionClickListener;
import com.voxel.sense.infomsg.module.task.execution.report.details.TaskReportContract;
import com.voxel.sense.infomsg.module.task.execution.report.details.DetailsReportActivity;
import com.voxel.sense.infomsg.view.decoration.ItemDivider;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;
import com.voxel.sense.infomsg.view.titlebar.TextTitleBar;

import butterknife.BindView;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.api.response.TaskReportListEntity;

/**
 * 任务报表列表
 */
public class ReportListActivity extends BViewActivity<ReportListContract.Presenter>
        implements ReportListContract.View<TaskReportListEntity> {

    @BindView(R.id.titlebar)
    TextTitleBar titlebar;
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;
    @BindView(R.id.empty_view)
    EmptyView empty_view;

    private ReportListAdapter mAdapter;
    private String mTaskNumb; //任务编号

    public static void show(Context context, String taskNumb) {
        if (TextUtils.isEmpty(taskNumb)) {
            return;
        }
        Intent intent = new Intent(context, ReportListActivity.class);
        intent.putExtra(IntentConstants.STRING_TYPE, taskNumb);
        context.startActivity(intent);

    }

    @Override
    protected ReportListPresenter initPresenter() {
        return new ReportListPresenter(this);
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_report_list;
    }

    @Override
    protected void initData() {
        super.initData();
        titlebar.setTitleText("报表列表");
        mTaskNumb = getIntent().getStringExtra(IntentConstants.STRING_TYPE);
        mAdapter = new ReportListAdapter();
        recycle_view.setLayoutManager(new LinearLayoutManager(this));
        recycle_view.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider_1dp_pink));
        recycle_view.setAdapter(mAdapter);

        empty_view.bind(recycle_view);
        empty_view.setRefreshBtnVisible(true);
        //加载数据
        empty_view.triggerLoading();
        mPresenter.doLoadReportList(mTaskNumb);
    }


    @Override
    protected void initEven() {
        super.initEven();
        empty_view.addRefreshBtnEvent(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //加载数据
                empty_view.triggerLoading();
                mPresenter.doLoadReportList(mTaskNumb);
            }
        });

        mAdapter.setListener(new RecyclerAdapter.AdapterListenerImpl<TaskReportListEntity>() {
            @Override
            public void onItemClick(RecyclerAdapter.ViewHolder holder, TaskReportListEntity entity) {
                super.onItemClick(holder, entity);
                String param2 = entity.getParam2();
                String formCode = entity.getFormCode();
                int status = (true + "").equals(param2) ? TaskReportContract.ALTER_REPORT : TaskReportContract.UP_REPORT;
                DetailsReportActivity.show(ReportListActivity.this, formCode, status, mTaskNumb);
            }
        });

        mAdapter.setDeleteClickListener(new AddItemPortionClickListener<TaskReportListEntity>() {
            @Override
            public void onItemChildViewClick(View childView, int position, TaskReportListEntity entity) {
                DialogImpl.getInstance().showChooseDialog(ReportListActivity.this, "", "是否删除已上传的报表", new DialogCallBack() {
                    @Override
                    public void onClick(int what) {
                        if (what == DialogImpl.YES) {
                            mPresenter.doDeleteExistReport(mTaskNumb, entity.getFormCode());
                        }
                    }
                });


            }
        });
    }

    @Override
    public RecyclerAdapter getRecyclerViewAdapter() {
        return mAdapter;
    }

    @Override
    public void onAdapterDataChanged() {
        if (mAdapter.getItemCount() > 0) {
            empty_view.triggerOk();
        } else if (!NetworkUtils.isConnected()) {
            empty_view.triggerNetError();
        } else {
            empty_view.triggerEmpty();
        }
    }

    @Override
    public void showEmptyView(String desc, boolean btnVisible) {
    }


}
